package com.manor.paradise.dialog

import android.content.Context
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.common.utils.scaleDown
import com.manor.paradise.R
import com.manor.paradise.bean.MineInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.manor.paradise.view.MineInfoListener
import kotlinx.android.synthetic.main.dialog_wozard_details_popup.view.*
import java.math.BigDecimal

/**
 * 精灵详情
 * @property tag String
 * @constructor
 */
@Suppress("UsePropertyAccessSyntax")
class WizardDetailsPopup(var mineInfoBean: MineInfoBean, context: Context) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_wozard_details_popup
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        tv_name.setText(mineInfoBean.name.toString())
        tv_1.setText("孵化所需米粒/能量：" + mineInfoBean.price + "个")
        tv_2.setText("养殖到期获得米粒：" + mineInfoBean.totalCrystalOre.scaleDown(4) + "个")
        tv_3.setText("养殖完成后产效率：" + mineInfoBean.returnRate)
        tv_4.setText("孵化后所得活跃度：" + mineInfoBean.anima + "")
        tv_5.setText("可同时养殖的数量：" + mineInfoBean.sameNum + "个")
        tv_6.setText("本次养殖活跃有效期：" + mineInfoBean.activeDuration + "天")
        tv_7.setText("养殖有效期：" + mineInfoBean.effectiveDuration + "天")

        tv_lmjl.text = mineInfoBean.contents
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    setPopDialog(mineInfoBean.id)
                    dismiss()
                }
            }
    }

    private fun setPopDialog(id: BigDecimal) {
        ElvesHttpUtil.getMineralInfo(id, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var mineInfoBean: MineInfoBean = GsonUtil.gsonToBean<MineInfoBean>(info, MineInfoBean::class.java)
                XPopup.Builder(context)
                        .hasShadowBg(true)
                        .dismissOnTouchOutside(true)
                        .hasStatusBarShadow(false) //启用状态栏阴影
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .asCustom(HireElvesPopup(mineInfoBean, context))
                        .show()
//                XPopup.Builder(context)
//                        .hasShadowBg(true)
//                        .dismissOnTouchOutside(true)
//                        .hasStatusBarShadow(false) //启用状态栏阴影
//                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
//                        .asCustom(MineInfoPopup(mineInfoBean, context, MineInfoListener { position ->
//                            if (position == 0) {
//                                XPopup.Builder(context)
//                                        .hasShadowBg(true)
//                                        .dismissOnTouchOutside(true)
//                                        .hasStatusBarShadow(false) //启用状态栏阴影
//                                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
//                                        .asCustom(WizardDetailsPopup(mineInfoBean, context))
//                                        .show()
//                            } else {
//                                if (position == 1) {
//                                    XPopup.Builder(context)
//                                            .hasShadowBg(true)
//                                            .dismissOnTouchOutside(true)
//                                            .hasStatusBarShadow(false) //启用状态栏阴影
//                                            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
//                                            .asCustom(HireElvesPopup(mineInfoBean, context))
//                                            .show()
//                                }
//                            }
//                        }))
//                        .show()
            }

            override fun onError() {
                super.onError()

            }
        })

    }


    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SPIRIT_INFO)
        super.onDestroy()
    }

}